#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''eof
name:3年内申请人历史失信情况
code:IG_DishonestyInThree
tableName:
columnName:
groups:准入>企业通用
dependencies:EG
type:常用指标
dataSourceType:在线指标
description:
eof'''

'''
ratingRequest 中legalPerson申请人姓名 legalPersonIdCard 申请人ID
与EG中EG_case 诉讼 shixing表 中 pname 和 pname_id 分别匹配
'''

import sys
import pandas as pd
import datetime
import re


reload(sys)
sys.setdefaultencoding('utf-8')

last_year_list = [(datetime.datetime.now() - pd.tseries.offsets.DateOffset(years=i)).strftime('%Y-%m-%d')\
                    for i in [3 , 0]]

def Ind_Dishonesty_3Yr():
    null_type_list = ['', None, 'null', 'Null', 'NULL', '/', ' ']
    count_list = set()
    try:
        applicant_name = ratingRequest['legalPerson']
        applicant_id = ratingRequest['legalPersonIdCard']
        case = EG['srcData'][0]['EG_case']['data']
        companyName = EG['srcData'][0]['EG']['results'][0]['jbxx']['company_name']
        if len(case) >=1:
            for i in case:
                try:
                    if i.get('bbd_table') not in null_type_list:
                        if i['bbd_table'] == 'dishonesty':
                            if Formate(i.get('pubdate')) is not None and i.get('case_code') not in null_type_list \
                                    and i.has_key('pname') and i.has_key('pname_id'):
                                if (i.get('idtype') == u'身份证' or (i['pname'] == applicant_name and i['pname'] != companyName) or
                                    (i['pname_id'] not in null_type_list and re.compile(i['pname_id'].replace('*', '.*')).match(applicant_id) is not None)) \
                                    and Formate(i['pubdate']) > last_year_list[0] \
                                    and Formate(i['pubdate']) <= last_year_list[1]:
                                        count_list.add(Formate(i['pubdate']) + i['case_code'])
                                else:
                                    count_list.add(0)
                            else:
                                count_list.add(None)
                        else:
                            continue
                    else:
                        count_list.add(None)
                except:
                    count_list.add(None)
                    continue

            if  None in count_list and  len(count_list) == 1 :
                return u'缺失值'
            else:
                count_nonone = [i for i in count_list if i!=0 and i!=None]
                if len(count_nonone) != 0:
                    return len(count_nonone)
                else:
                    return 0
        else:
            return 0
    except:
        return u'缺失值'


def Formate(time):
    import re
    import datetime
    r = re.compile(r'\D+')
    try:
        date = datetime.datetime.strptime(time, '%Y.%m.%d').strftime('%Y-%m-%d')
    except:
        try:
            date = datetime.datetime.strptime(time, '%Y-%m-%d').strftime('%Y-%m-%d')
        except:
            try:
                date = datetime.datetime.strptime(time, '%Y/%m/%d').strftime('%Y-%m-%d')
            except:
                try:
                    tmp = r.sub('', time)
                    date = datetime.datetime.strptime(tmp, '%Y%m%d').strftime('%Y-%m-%d')
                except:
                    date = None
    return date


result = Ind_Dishonesty_3Yr()